package com.me.spider;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * 
 *
 * @author ME
 * @date   2020/1/7
 */
public class TestHttpClient {

    public static void main(String[] args) {
        new TestHttpClient().doGet("http://www.baidu.com", null);
    }

    public void doGet(String uri, Map<String, String> args) {
        // 获得Http客户端
        ;

        // 响应模型
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            // 构建 URI
            URIBuilder uriBuilder = new URIBuilder(uri);

            // 设置参数
            if (args != null) {
                // args.entrySet().forEach(entry -> {})
                // args.forEach((key, value) -> uriBuilder.addParameter(key, value));
                args.forEach(uriBuilder::addParameter);
            }

            // 创建Get请求
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            //伪装浏览器请求
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");

            // 由客户端执行(发送)Get请求
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                }
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doPost(String uri, Map<String, String> args) {
        // 获得Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 构建 URI
            URIBuilder uriBuilder = new URIBuilder(uri);
            // 设置参数
            if (args != null) {
                args.entrySet().forEach(entry -> {
                    uriBuilder.addParameter(entry.getKey(), entry.getValue());
                });
            }

            HttpRequestBase httpPost = new HttpPost(uriBuilder.build());
            //伪装浏览器请求
            // httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");

            response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity responseEntity = response.getEntity();
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
